import 'dart:math';

import 'package:elephant_fish/dao/register_dao.dart';
import 'package:elephant_fish/http/core/hi_error.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:elephant_fish/util/string_util.dart';
import 'package:elephant_fish/widget/text_fild.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class RegisterPage extends StatefulWidget {
  const RegisterPage({Key key}) : super(key: key);

  @override
  _RegisterPageState createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
  TextEditingController _nameController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();
  TextEditingController _rePasswordController = TextEditingController();
  TextEditingController _vcodeController = TextEditingController();
  TextEditingController _shareController = TextEditingController();
  //分别定义两个输入框的焦点 用于切换焦点
  final FocusNode _nodeName = FocusNode();
  final FocusNode _nodePwd = FocusNode();
  final FocusNode _reNodePwd = FocusNode();
  final FocusNode _shareCode = FocusNode();
  final FocusNode _nodeVCode = FocusNode();
  bool _isClick = false;
  bool registerEnable = false;
  bool sample = false;
  String phone;
  String code;
  String password;
  String confirm;
  String shareCode;


  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    return Scaffold(
      resizeToAvoidBottomInset: false,
      body: SingleChildScrollView(
        child: Container(
          alignment: Alignment.topCenter,
          width: MediaQuery.of(context).size.width,
          height: MediaQuery.of(context).size.height,
          decoration: BoxDecoration(
            image: new DecorationImage(
              fit: BoxFit.cover,
              image: AssetImage('assets/images/register_bg.png'),
            ),
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [Color(0xffff9182), Color(0xffff849b)],
            ),

          ),
          child: Stack(
            children: [
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(top: SizeUtil.px(179)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Image.asset('assets/images/logo.png',width: SizeUtil.px(57.72),height: SizeUtil.px(56),),
                      SizedBox(width: SizeUtil.px(25),),
                      Text('象鱼直播',style: TextStyle(fontSize: SizeUtil.px(56),color: Color(0xffffffff))),
                    ],
                  ),
                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(60),right: SizeUtil.px(60),top: SizeUtil.px(366)),
                  child: MyTextField(
                    focusNode: _nodeName,
                    placeHolder: '请输入您的手机号',
                    maxLength: 11,
                    keyboardType: TextInputType.phone,
                    controller: _nameController,
                    onChanged: (text){
                      phone = text;
                      checkInput();
                    },
                  ),
                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(60),right: SizeUtil.px(60),top: SizeUtil.px(486)),
                  child: MyTextField(
                  focusNode: _nodeVCode,
                  placeHolder: '请输入验证码',
                  maxLength: 6,
                  controller: _vcodeController,
                  getVCode: (text){
                    code = text;
                  },
                  keyboardType: TextInputType.phone,
                    onChanged: (text){
                      code = text;
                      checkInput();
                    },
                ),

              ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(60),right: SizeUtil.px(60),top: SizeUtil.px(606)),
                  child: MyTextField(
                    focusNode: _nodePwd,
                    placeHolder: '请输入密码',
                    controller: _passwordController,
                    isInputPwd: true,
                    onChanged: (text){
                      password = text;
                      checkInput();
                    },
                  ),

                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(60),right: SizeUtil.px(60),top: SizeUtil.px(726)),
                  child: MyTextField(
                    focusNode: _reNodePwd,
                    placeHolder: '请重复输入密码',
                    controller: _rePasswordController,
                    isInputPwd: true,
                    onChanged: (text){
                      confirm = text;
                      checkInput();
                    },
                  ),

                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(60),right: SizeUtil.px(60),top: SizeUtil.px(846)),
                  child: MyTextField(
                    focusNode: _shareCode,
                    placeHolder: '请输入邀请码',
                    controller: _shareController,
                    onChanged: (text){
                      shareCode = text;
                    },
                  ),

                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(left: SizeUtil.px(60),right: SizeUtil.px(60),top: SizeUtil.px(1070)),
                  child: Container(
                    width: SizeUtil.px(650),
                    height: SizeUtil.px(91),
                    child: RaisedButton(
                      shape: RoundedRectangleBorder(
                        side: BorderSide.none,
                        borderRadius: BorderRadius.all(Radius.circular(30)),
                      ),
                      color: Color(0xffe62d6c),
                      disabledColor: Color(0xffe62d6c),
                      child: Text(
                        '立即注册',
                        style: TextStyle(fontSize: SizeUtil.px(36),color: Color(0xffffffff)),
                      ),
                      onPressed: (){
                        checkInput();
                        checkSample();
                        if(_isClick){
                          if(registerEnable){
                            setState(() {
                              send();
                            });
                          }else{
                            setState(() {
                              TxUtils.showErrorToast('您填写的信息不正确', context);
                            });
                          }
                        }else{
                          setState(() {
                            TxUtils.showErrorToast('您还没有同意用户协议', context);
                          });
                        }

                      },
                    ),

                  )
                ),
              ),
              Align(
                alignment: Alignment.topCenter,
                child: Padding(
                  padding: EdgeInsets.only(top: SizeUtil.px(1181)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Checkbox(value: _isClick, onChanged: (bool value){
                        setState(() {
                          _isClick = value;
                        });
                      },checkColor: Color(0xffffffff) ,materialTapTargetSize:MaterialTapTargetSize.shrinkWrap),
                      SizedBox(width: SizeUtil.px(5),),
                      Text('我已看过并接受《用户协议》',style: TextStyle(fontSize: SizeUtil.px(24),color: Color(0xffffffff))),

                    ],
                  ),
                ),
              ),
            ],
          ),

        ),
      ),
    );
  }

  void send() async {
    try {
      var result;
      if(shareCode != '' && shareCode != null){
         result = await RegisterDao.register(phone,code,password,confirm,referrer_id: shareCode);
      }else{
         result = await RegisterDao.register(phone,code,password,confirm);
      }
      print(result);
      if (result['code'] == 0) {
        print('注册成功');
        TxUtils.showToast('注册成功',context);
        Navigator.of(context).pop();
        Navigator.pushNamed(context, '/login');
      } else {
        print(result['message']);
        TxUtils.showErrorToast(result['message'],context);
      }
    } on NeedAuth catch (e) {
      print(e);
      TxUtils.showErrorToast(e.message,context);
    } on HiNetError catch (e) {
      print(e);
      TxUtils.showErrorToast(e.message,context);
    }
  }

  void checkInput() {
    bool enable;
    if (isNotEmpty(phone) && isNotEmpty(password) && isNotEmpty(code) && isNotEmpty(confirm) && sample) {
      enable = true;
    } else {
      enable = false;
    }
    setState(() {
      registerEnable = enable;
    });
  }

  void checkSample() {
    bool enable;
    if(Comparable.compare(password, confirm) == 0){
      enable = true;
    }else{
      enable = false;
    }
    setState(() {
      if(! enable){
        TxUtils.showToast('两次输入的密码不一致', context);
      }
      sample = enable;
    });
  }
}
